package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.domain.PurchaseListGoodsMapper;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PurchaseListGoodsServiceimpl implements PurchaseListGoodsService {
    @Autowired
    PurchaseListGoodsMapper purchaseListGoodsMapper;

    //进货单保存
    @Override
    public void save(PurchaseList purchaseList, String purchaseListGoodsStr) {
          purchaseList.setUserId(1);
          //进行保存  回显主键id
          purchaseListGoodsMapper.savepurchaseList(purchaseList);
        Gson  gson=new Gson();
        List<PurchaseListGoods> listGood = gson.fromJson(purchaseListGoodsStr, new TypeToken<List<PurchaseListGoods>>() {
        }.getType());
        for (PurchaseListGoods purchaseListGoods : listGood) {
               purchaseListGoods.setPurchaseListId(purchaseList.getPurchaseListId());
               //进行保存
                purchaseListGoodsMapper.savepurchaseListGoods(purchaseListGoods);
        }

    }
}
